package com.ruicar.afs.cloud.apply.commission.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.apply.business.service.TSysParamConfigService;
import com.ruicar.afs.cloud.apply.commission.condition.CommissionFileCondition;
import com.ruicar.afs.cloud.apply.commission.service.ApplyToCommissionService;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.bizcommon.commission.dto.basecommission.*;
import com.ruicar.afs.cloud.bizcommon.commission.dto.currency.ConfirmSendMailDto;
import com.ruicar.afs.cloud.bizcommon.commission.dto.currency.GetCurrencyRewardListDto;
import com.ruicar.afs.cloud.bizcommon.commission.dto.discount.DiscountListDto;
import com.ruicar.afs.cloud.bizcommon.commission.dto.reward.*;
import com.ruicar.afs.cloud.bizcommon.commission.service.impl.CommissionServiceImpl;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @description: 归档邮寄
 * @author: panhj
 * @date: 2020/6/6 16:09
 */
@AllArgsConstructor
@RestController
@RequestMapping("/commission")
public class ApplyToCommissionController {
    private ApplyToCommissionService applyToCommissionService;
    CommissionServiceImpl commissionService;
    private TSysParamConfigService tSysParamConfigService;
    /**
     * 获取状态列表
     * @return
     */
    @ApiOperation("获取状态列表")
    @PostMapping("/getstatus")
    public IResponse getstatus(){
        return applyToCommissionService.getstatus();
    }
    /**
     * 获取奖励类型列表
     * @return
     */
    @ApiOperation("获取状态列表")
    @PostMapping("/rewardtypes")
    public IResponse rewardtypes(){
        return applyToCommissionService.rewardtypes();
    }
    /**
     * 获取摊销奖励下发信息列表
     * @return
     */
    @ApiOperation("获取摊销奖励下发信息列表")
    @PostMapping("/getlist")
    public IResponse getlist(@RequestBody GetRewardListDto dto){
        String userId=tSysParamConfigService.getParamValue("commission","userId","");
        JSONObject jsonObject=commissionService.getRewordList(dto,userId, ApplyConstants.ICOS);
        IPage iPage=new Page();
        JSONArray jsonArray=jsonObject.getJSONObject("result").getJSONArray("data");
        List list=JSONObject.parseArray(jsonArray.toJSONString());
        iPage.setRecords(list);
        iPage.setTotal(jsonObject.getJSONObject("result").getInteger("recordsTotal"));
        iPage.setSize(jsonObject.getJSONObject("result").getInteger("pageSize"));
        iPage.setCurrent(jsonObject.getJSONObject("result").getInteger("currPage"));
        return IResponse.success(iPage);
    }
    /**
     * 经销商确认已打款
     * @return
     */
    @ApiOperation("经销商确认已打款")
    @PostMapping("/confirmReceiveMoney")
    public IResponse confirmReceiveMoney(@RequestBody ConfirmReceiveMoneyDto dto){
        JSONObject jsonObject=applyToCommissionService.confirmReceiveMoney(dto);
        return IResponse.success(jsonObject);
    }
    /**
     * 获取合同信息列表
     * @return
     */
    @ApiOperation("合同信息列表")
    @PostMapping("/getContractList")
    public IResponse getContractList(@RequestBody GetContractListDto dto){
        JSONObject jsonObject=applyToCommissionService.getContractList(dto);
        IPage iPage=new Page();
        JSONArray jsonArray=jsonObject.getJSONObject("result").getJSONArray("data");
        List list=JSONObject.parseArray(jsonArray.toJSONString());
        iPage.setRecords(list);
        iPage.setTotal(jsonObject.getJSONObject("result").getInteger("recordsTotal"));
        iPage.setSize(jsonObject.getJSONObject("result").getInteger("pageSize"));
        iPage.setCurrent(jsonObject.getJSONObject("result").getInteger("currPage"));
        iPage.setPages(jsonObject.getJSONObject("result").getInteger("totalPage"));
        return IResponse.success(iPage);
    }
    /**
     * 保存邮寄信息
     * @return
     */
    @ApiOperation("保存邮寄信息")
    @PostMapping("/confirmExpress")
    public IResponse confirmExpress(@RequestBody ConfirmExpressDto dto){
        JSONObject jsonObject=applyToCommissionService.confirmExpress(dto);
        return IResponse.success(jsonObject);
    }
    /**
     * 导出合同
     */
    @ApiOperation("导出合同")
    @PostMapping("/export")
    public void export(@RequestBody CommissionFileCondition commissionFileCondition){
        commissionService.downFile(commissionFileCondition.getId(),"佣金文件",commissionFileCondition.getType());
    }
    /**
     * 获取通用奖励类型列表
     * @return
     */
    @ApiOperation("获取通用奖励类型列表")
    @PostMapping("/currencyRewardTypes")
    public IResponse currencyRewardTypes(){
        return applyToCommissionService.currencyRewardTypes();
    }
    /**
     * 获取季度列表
     * @return
     */
    @ApiOperation("获取季度列表")
    @PostMapping("/getQuarter")
    public IResponse getQuarter(){
        return applyToCommissionService.getQuarter();
    }
    /**
     * 获取通用奖励下发信息列表
     * @return
     */
    @ApiOperation("获取通用奖励下发信息列表")
    @PostMapping("/getCurrencyRewardList")
    public IResponse getCurrencyRewardList(@RequestBody GetCurrencyRewardListDto dto){
        String userId=tSysParamConfigService.getParamValue("commission","userId","");
        JSONObject jsonObject=commissionService.getCurrencyRewordList(dto,userId, ApplyConstants.ICOS);
        IPage iPage=new Page();
        JSONArray jsonArray=jsonObject.getJSONObject("result").getJSONArray("data");
        List list=JSONObject.parseArray(jsonArray.toJSONString());
        iPage.setRecords(list);
        iPage.setTotal(jsonObject.getJSONObject("result").getInteger("recordsTotal"));
        iPage.setSize(jsonObject.getJSONObject("result").getInteger("pageSize"));
        iPage.setCurrent(jsonObject.getJSONObject("result").getInteger("currPage"));
        return IResponse.success(iPage);
    }
    /**
     * 经销商确认已打款
     * @return
     */
    @ApiOperation("经销商确认已打款")
    @PostMapping("/receiptMoney")
    public IResponse receiptMoney(@RequestBody ConfirmReceiveMoneyDto dto){
        JSONObject jsonObject=applyToCommissionService.receiptMoney(dto);
        return IResponse.success(jsonObject);
    }
    /**
     * 获取经销商详情
     * @return
     */
    @ApiOperation("获取经销商详情")
    @PostMapping("/getbasedealerinfo")
    public IResponse getbasedealerinfo(@RequestBody DealerinfoDto dealerinfoDto){
        JSONObject jsonObject=applyToCommissionService.getbasedealerinfo(dealerinfoDto);
        return IResponse.success(jsonObject);
    }
    /**
     * 保存邮寄信息
     * @return
     */
    @ApiOperation("保存邮寄信息")
    @PostMapping("/confirmSendMail")
    public IResponse confirmSendMail(@RequestBody ConfirmSendMailDto dto){
        JSONObject jsonObject=applyToCommissionService.confirmSendMail(dto);
        return IResponse.success(jsonObject);
    }
    /**
     * 获取返利列表
     * @return
     */
    @ApiOperation("获取返利列表")
    @PostMapping("/getReabteList")
    public IResponse getReabteList(@RequestBody GetlistDto dto){
        String userId=tSysParamConfigService.getParamValue("commission","userId","");
        JSONObject jsonObject=commissionService.getlist(dto,userId, ApplyConstants.ICOS);
        IPage iPage=new Page();
        JSONArray jsonArray=jsonObject.getJSONObject("result").getJSONArray("data");
        List list=JSONObject.parseArray(jsonArray.toJSONString());
        iPage.setRecords(list);
        iPage.setTotal(jsonObject.getJSONObject("result").getInteger("recordsTotal"));
        iPage.setSize(jsonObject.getJSONObject("result").getInteger("pageSize"));
        iPage.setCurrent(jsonObject.getJSONObject("result").getInteger("currPage"));
        return IResponse.success(iPage);
    }
    /**
     * 返利确认已打款
     * @return
     */
    @ApiOperation("经销商确认已打款")
    @PostMapping("/rebateReceiptMoney")
    public IResponse rebateReceiptMoney(@RequestBody ReceiptDto dto){
        JSONObject jsonObject=applyToCommissionService.rebateReceiptMoney(dto);
        return IResponse.success(jsonObject);
    }
    /**
     * 返利保存邮寄信息
     * @return
     */
    @ApiOperation("保存邮寄信息")
    @PostMapping("/rebateConfirmExpress")
    public IResponse rebateConfirmExpress(@RequestBody ConfirmExpressDto dto){
        JSONObject jsonObject=applyToCommissionService.rebateConfirmExpress(dto);
        return IResponse.success(jsonObject);
    }
    /**
     * 获取经销商详情
     * @return
     */
    @ApiOperation("获取经销商详情")
    @PostMapping("/getBaseDealerInfo")
    public IResponse getBaseDealerInfo(@RequestBody DealerinfoDto dto){
        JSONObject jsonObject=applyToCommissionService.getBaseDealerInfo(dto);
        return IResponse.success(jsonObject);
    }
    /**
     * 获取返利合同信息列表
     * @return
     */
    @ApiOperation("获取返利合同信息列表")
    @PostMapping("/getBaseContractList")
    public IResponse getBaseContractList(@RequestBody GetbasecontractlistDto dto){
        JSONObject jsonObject=applyToCommissionService.getBaseContractList(dto);
        IPage iPage=new Page();
        JSONArray jsonArray=jsonObject.getJSONObject("result").getJSONArray("data");
        List list=JSONObject.parseArray(jsonArray.toJSONString());
        iPage.setRecords(list);
        iPage.setTotal(jsonObject.getJSONObject("result").getInteger("recordsTotal"));
        iPage.setSize(jsonObject.getJSONObject("result").getInteger("pageSize"));
        iPage.setCurrent(jsonObject.getJSONObject("result").getInteger("currPage"));
        iPage.setPages(jsonObject.getJSONObject("result").getInteger("totalPage"));
        return IResponse.success(iPage);
    }
    @ApiOperation("获取返利列表")
    @PostMapping("/getDiscountList")
    public IResponse getDiscountList(@RequestBody DiscountListDto dto){
        String userId=tSysParamConfigService.getParamValue("commission","userId","");
        JSONObject jsonObject=commissionService.getDiscountList(dto,userId, ApplyConstants.ICOS);
        IPage iPage=new Page();
        JSONArray jsonArray=jsonObject.getJSONObject("result").getJSONArray("data");
        List list=JSONObject.parseArray(jsonArray.toJSONString());
        iPage.setRecords(list);
        iPage.setTotal(jsonObject.getJSONObject("result").getInteger("recordsTotal"));
        iPage.setSize(jsonObject.getJSONObject("result").getInteger("pageSize"));
        iPage.setCurrent(jsonObject.getJSONObject("result").getInteger("currPage"));
        return IResponse.success(iPage);
    }
    /**
     * 获取贴息经销商详情
     * @return
     */
    @ApiOperation("获取贴息经销商详情")
    @PostMapping("/getDiscountInfo")
    public IResponse getDiscountInfo(@RequestBody DealerinfoDto dealerinfoDto){
        JSONObject jsonObject=applyToCommissionService.getDiscountInfo(dealerinfoDto);
        return IResponse.success(jsonObject);
    }
    /**
     * 获取贴息合同信息列表
     * @return
     */
    @ApiOperation("获取贴息合同信息列表")
    @PostMapping("/getDiscountMainList")
    public IResponse getDiscountMainList(@RequestBody GetbasecontractlistDto dto){
        JSONObject jsonObject=applyToCommissionService.getDiscountList(dto);
        IPage iPage=new Page();
        JSONArray jsonArray=jsonObject.getJSONObject("result").getJSONArray("data");
        List list=JSONObject.parseArray(jsonArray.toJSONString());
        iPage.setRecords(list);
        iPage.setTotal(jsonObject.getJSONObject("result").getInteger("recordsTotal"));
        iPage.setSize(jsonObject.getJSONObject("result").getInteger("pageSize"));
        iPage.setCurrent(jsonObject.getJSONObject("result").getInteger("currPage"));
        iPage.setPages(jsonObject.getJSONObject("result").getInteger("totalPage"));
        return IResponse.success(iPage);
    }
    /**
     * 获取返利回执退回列表
     * @return
     */
    @ApiOperation("获取返利回执退回列表")
    @PostMapping("/getRebateReturnList")
    public IResponse getRebateReturnList(@RequestBody AttachbacklistDto dto){
        String userId=tSysParamConfigService.getParamValue("commission","userId","");
        JSONObject jsonObject=commissionService.attachbacklist(dto,userId, ApplyConstants.ICOS);
        IPage iPage=new Page();
        JSONArray jsonArray=jsonObject.getJSONObject("result").getJSONArray("data");
        List list=JSONObject.parseArray(jsonArray.toJSONString());
        iPage.setRecords(list);
        iPage.setTotal(jsonObject.getJSONObject("result").getInteger("recordsTotal"));
        iPage.setSize(jsonObject.getJSONObject("result").getInteger("pageSize"));
        iPage.setCurrent(jsonObject.getJSONObject("result").getInteger("currPage"));
        return IResponse.success(iPage);
    }
    /**
     * 获取推销回执退回列表
     * @return
     */
    @ApiOperation("获取推销回执退回列表")
    @PostMapping("/getCreditReturnList")
    public IResponse getCreditReturnList(@RequestBody CreditRewardReturnListDto creditRewardReturnListDto){
        String userId=tSysParamConfigService.getParamValue("commission","userId","");
        JSONObject jsonObject=commissionService.attachBackList(creditRewardReturnListDto,userId, ApplyConstants.ICOS);
        IPage iPage=new Page();
        JSONArray jsonArray=jsonObject.getJSONObject("result").getJSONArray("data");
        List list=JSONObject.parseArray(jsonArray.toJSONString());
        iPage.setRecords(list);
        iPage.setTotal(jsonObject.getJSONObject("result").getInteger("recordsTotal"));
        iPage.setSize(jsonObject.getJSONObject("result").getInteger("pageSize"));
        iPage.setCurrent(jsonObject.getJSONObject("result").getInteger("currPage"));
        return IResponse.success(iPage);
    }

    /**
     * 获取通用奖励回执退回列表
     * @return
     */
    @ApiOperation("获取通用奖励回执退回列表")
    @PostMapping("/getCurrencyReturnList")
    public IResponse getCurrencyReturnList(@RequestBody CurrencyRewardReturnListDto currencyRewardReturnListDto){
        String userId=tSysParamConfigService.getParamValue("commission","userId","");
        JSONObject jsonObject=commissionService.attachBackCurrencyList(currencyRewardReturnListDto,userId, ApplyConstants.ICOS);
        IPage iPage=new Page();
        JSONArray jsonArray=jsonObject.getJSONObject("result").getJSONArray("data");
        List list=JSONObject.parseArray(jsonArray.toJSONString());
        iPage.setRecords(list);
        iPage.setTotal(jsonObject.getJSONObject("result").getInteger("recordsTotal"));
        iPage.setSize(jsonObject.getJSONObject("result").getInteger("pageSize"));
        iPage.setCurrent(jsonObject.getJSONObject("result").getInteger("currPage"));
        return IResponse.success(iPage);
    }

    /**
     * 上传文件
     * @return
     */
    @ApiOperation("上传文件")
    @PostMapping("/uploadFile")
    public IResponse uploadFile(@RequestBody MultipartFile file ,String id){
        applyToCommissionService.uploadFile(file,id);
        return IResponse.success(true);
    }

}
